<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
class Api_MailController extends Zend_Rest_Controller
{
	public function init()
	{
		$this->_helper->viewRenderer->setNoRender(true);
	}


	public function deleteAction() {

	}

	public function postAction() {
		try{
			$user_mapper = new Api_Model_UserMapper();
			$rawBody = $this->getRequest()->getRawBody();
			$data = Zend_Json::decode($rawBody);

			if ($data['email'] != ""){

				$password = $user_mapper->mail($data);

				//Create SMTP connection
				$configInfo = array(
                    'auth'     => 'login',            
                    'username' => 'youtour123@gmail.com',
                    'password' => 'haideptrai',
                    'ssl'      => 'ssl',
                    'port'     => 465
				);
				$smtpHost = new Zend_Mail_Transport_Smtp('smtp.gmail.com',$configInfo);
				Zend_Mail::setDefaultTransport($smtpHost);
				//Create Zend_Mail
				$mail = new Zend_Mail('UTF-8');
				//Initialize parameters.
				$mail->setFrom ('youtour123@gmail.com', 'YouTour');
				$mail->addTo($data['email']);
				$mail->setSubject('Forgot Password');
				$mail->setBodyText('Your new password is'.$password);
				//Send Email
				$mail->send($smtpHost);
				throw new Exception(json_encode(array('meta' => 'OK')),200);

			} else {
				throw new Exception(json_encode(array('meta' => 'Bad request')),400);
			}

		}catch(Exception $e){
			$this->getresponse()->appendBody($e->getMessage());
			$this->getResponse()->setHttpResponseCode($e->getCode());
		}
	}

	public function headAction() {

	}

	public function indexAction() {

	}

	public function getAction() {

	}

	public function putAction() {

	}
}

